package com.lovo.bbs.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.util.HashMap;

import javax.naming.NamingException;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.lovo.bbs.bo.LoginBo;
import com.lovo.bbs.vo.LoginVo;

/**
 * 登录Servlet
 * 
 * @author tiancen2001
 * 
 */
public class LoginServlet extends HttpServlet {
	private static final long serialVersionUID = -5219394375184581017L;

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		this.doPost(request, response);
	}

	/* (non-Javadoc)
	 * @see javax.servlet.http.HttpServlet#doPost(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
	 */
	@SuppressWarnings("unchecked")
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {


		String userName = request.getParameter("username");
		String password = request.getParameter("password");
		
		String type=request.getParameter("type");//登录类型
		if(type==null||"".equals(type)){
			type = "member";
		}
		
		int asType = LoginBo.TYPE_OF_MEMBER;//普通会员登录
	    if("moderator".equals(type)){
	    	asType=LoginBo.TYPE_OF_MODERATOR;//版主登录
	    }else if("admin".equals(type)){
	    	asType=LoginBo.TYPE_OF_ADMIN;//管理员登陆
	    }else if("member".equals(type)){
	    	asType=LoginBo.TYPE_OF_MEMBER;//普通会员登录
	    }
	    
		HttpSession session = request.getSession();

		String msg = null;// 返回消息

		// 封装登录信息
		LoginVo loginvo = new LoginVo();
		loginvo.setUserName(userName);
		loginvo.setPassword(password);

		try {
			// 登录校验
			if (new LoginBo().checkUser(loginvo,asType)) {
				// 记录用户已登录状态
				session.setAttribute("loginvo", loginvo);
				//记录用户类型
				if(asType==LoginBo.TYPE_OF_MODERATOR){//版主
					session.setAttribute("moderator", loginvo.getUserName());
					session.removeAttribute("admin");
					session.removeAttribute("member");
				}else if(asType==LoginBo.TYPE_OF_ADMIN){
					session.setAttribute("admin", loginvo.getUserName());//管理员
					session.removeAttribute("member");
					session.removeAttribute("moderator");
				}else if(asType==LoginBo.TYPE_OF_MEMBER){
					session.setAttribute("member", loginvo.getUserName());//普通用户
					session.removeAttribute("admin");
					session.removeAttribute("moderator");
					//添加Cookie
					String autologin  = request.getParameter("autologin");
					if("true".equals(autologin)){
						Cookie cookieName = new Cookie("cookieName",userName);
						Cookie cookiepwd = new Cookie("cookiePassword",password);
						cookieName.setMaxAge(3600*24*14);
						cookiepwd.setMaxAge(3600*24*14);
						response.addCookie(cookieName);
						response.addCookie(cookiepwd);
					}
					
				}
				// 将用户加入在线列表
				ServletContext contex = request.getSession()
						.getServletContext();
				HashMap<String, LoginVo> onlineUser = null;
				onlineUser = (HashMap<String, LoginVo>) contex
						.getAttribute("onlineUser");
				onlineUser.put(loginvo.getUserName(), loginvo);
				contex.setAttribute("onlineUser", onlineUser);

				msg = "1"; // 登录成功时返回的信息
			} else {
				msg = "0";// 0为登录信息错误时显示的信息
			}
		} catch (NamingException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		}

		response.setContentType("text/html;charset=utf-8");
		response.setCharacterEncoding("utf-8");
		PrintWriter out = response.getWriter();
		out.println(msg);
		out.flush();
		out.close();
	}
}
